package org.tlipoca.smuserhistory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.constant.ExceptionConstant;
import org.genntii.smcommon.exception.*;
import org.genntii.smdomain.dto.UserHistoryDTO;
import org.genntii.smdomain.entity.SteamStore;
import org.genntii.smdomain.entity.UserHistory;
import org.genntii.smdomain.vo.ArticleDetailVO;
import org.genntii.smdomain.vo.UserHistoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tlipoca.smuserhistory.client.ArticleClient;
import org.tlipoca.smuserhistory.client.SteamStoreClient;
import org.tlipoca.smuserhistory.client.UserClient;
import org.tlipoca.smuserhistory.mapper.UserHistoryMapper;
import org.tlipoca.smuserhistory.service.UserHistoryService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserHistoryServiceImpl extends ServiceImpl<UserHistoryMapper, UserHistory> implements UserHistoryService {

    @Autowired
    private UserHistoryMapper userHistoryMapper;

    @Autowired
    private ArticleClient articleClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private SteamStoreClient steamStoreClient;

    @Override
    public ArrayList<UserHistoryVO> getUserArticleHistoryListService(String keyword, Long userId) {
        List<UserHistory> userHistories = userHistoryMapper.selectList(new QueryWrapper<UserHistory>().eq("user_id", userId).eq("appid",0));
        if (userHistories.isEmpty()){
            throw new HistoryNotFoundException(ExceptionConstant.HISTORY_NOT_FOUND_EXCEPTION);
        }
        ArrayList<Long> articleIds = new ArrayList<>();
        ArrayList<UserHistoryVO> userHistoryVOList = new ArrayList<>();

        for (UserHistory userHistory : userHistories) {
            articleIds.add(userHistory.getArticleId());
        }

        ArrayList<ArticleDetailVO> articleList = articleClient.getArticleListByIds(articleIds).getData();
        Map<Long, ArticleDetailVO> articleMap = new HashMap<>();
        for (ArticleDetailVO articleDetailVO : articleList) {
            articleMap.put(articleDetailVO.getId(), articleDetailVO);
        }

        for (UserHistory userHistory : userHistories) {
            Long articleId = userHistory.getArticleId();

            userHistoryVOList.add(UserHistoryVO.builder()
                            .id(userHistory.getId())
                            .userId(userHistory.getUserId())
                            .articleId(articleId)
                            .authorId(articleMap.get(articleId).getUserId())
                            .authorName(articleMap.get(articleId).getAuthorName())
                            .like(articleMap.get(articleId).getLike())
                            .dislike(articleMap.get(articleId).getDislike())
                            .title(articleMap.get(articleId).getTitle())
                            .cover(articleMap.get(articleId).getCover())
                            .content(articleMap.get(articleId).getContent())
                            .channelId(articleMap.get(articleId).getChannelId())
                            .channelName(articleMap.get(articleId).getChannelName())
                            .createTime(userHistory.getCreateTime())
                    .build());
        }

        return userHistoryVOList;
    }

    @Override
    public ArrayList<SteamStore> getUserStoreHistoryListService(String keyword, Long userId) {
        List<UserHistory> userHistories = userHistoryMapper.selectList(new QueryWrapper<UserHistory>().eq("user_id", userId).eq("article_id",0));
        if (userHistories.isEmpty()){
            throw new HistoryNotFoundException(ExceptionConstant.HISTORY_NOT_FOUND_EXCEPTION);
        }

        ArrayList<Integer> storeIds = new ArrayList<>();

        for (UserHistory userHistory : userHistories){
            storeIds.add(userHistory.getAppid());
        }

        return steamStoreClient.getSteamStoreBatchByIds(storeIds).getData();

    }

    @Override
    public void addUserHistoryService(Long userId, UserHistoryDTO userHistoryDTO) {

        if (userHistoryDTO.getAppid() != 0 && userHistoryDTO.getArticleId()!= 0){
            throw new MethodErrorException(ExceptionConstant.METHOD_DONT_ALLOWED);
        }

        if(!userClient.userExists(userId).getData()) {
            throw new UserNotFoundException(ExceptionConstant.USER_NOT_FOUND_EXCEPTION);
        }

        if(userHistoryDTO.getArticleId() != 0 &&  !articleClient.articleExists(userHistoryDTO.getArticleId()).getData()) {
            throw new ArticleNotFoundException(ExceptionConstant.ARTICLE_NOT_FOUND_EXCEPTION);
        }

        if(userHistoryDTO.getAppid() != 0 && !steamStoreClient.steamStoreExists(userHistoryDTO.getAppid()).getData()) {
            throw new SteamAppNotFoundException(ExceptionConstant.STEAM_APP_NOT_FOUND_EXCEPTION);
        }

        userHistoryMapper.insert(UserHistory.builder()
                        .userId(userId)
                        .appid(userHistoryDTO.getAppid())
                        .articleId(userHistoryDTO.getArticleId())
                .build());
    }
}
